package com.atguigu.cmsservice.service.impl;

import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.entity.vo.BannerVo;
import com.atguigu.cmsservice.mapper.CrmBannerMapper;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author wangshuai
 * @since 2021-03-04
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {
    @Override
    public Map<String,Object> showBannerList(Long page, Long limit, BannerVo bannerVo) {

        Page<CrmBanner> bannerPage = new Page<>(page,limit);

        QueryWrapper<CrmBanner> wrapperBanner = new QueryWrapper<>();

        String title = bannerVo.getTitle();

        if (!StringUtils.isEmpty(title)) {
            wrapperBanner.eq("title",title);
        }

        IPage<CrmBanner> pageBanner = baseMapper.selectPage(bannerPage,wrapperBanner);

        Long total = pageBanner.getTotal();

        List<CrmBanner> records = pageBanner.getRecords();

        Map map = new HashMap();

        map.put("total",total);

        map.put("list",records);

        return map;
    }

    @Cacheable(key="'selectIndexList'",value="banner")
    @Override
    public List<Map> bannerList() {

        List<Map> list = baseMapper.getBannerList();

        return list;
    }
}
